﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Controls
{
    public partial class TrustDistrictList : System.Web.UI.UserControl
    {
        private TrustDistrictBiz trustDistrictBiz = new TrustDistrictBiz();
        private int trustRegionId;
        private static int _flag;
        private static int _check;
        private static string _key = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["id"], out trustRegionId))
            {
                _flag = 0;
                _check = 0;
                LoadAllTrustDistrict();
            }
            LoadAllTrustDistrict();
        }

        private void LoadAllTrustDistrict()
        {
            grvTrustDistrictList.DataSource = trustDistrictBiz.GetTrustDistrictById1(trustRegionId);
            grvTrustDistrictList.DataBind();
        }


        protected void StartWithByNames(object sender, CommandEventArgs e)
        {
            if (CheckBoxInActive.Checked)
            {
                _check = 1;
                _flag = 1;
                _key = e.CommandName;
                grvTrustDistrictList.DataSource = trustDistrictBiz.StartWithByNames(_key, _flag, trustRegionId);
                grvTrustDistrictList.DataBind();
            }
            else
            {
                _check = 1;
                _flag = 0;
                _key = e.CommandName;
                grvTrustDistrictList.DataSource = trustDistrictBiz.StartWithByNames(_key, _flag, trustRegionId);
                grvTrustDistrictList.DataBind();
            }
        }

        protected void LinkButtonTrustRegionName_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/TrustDistrictDetails.aspx?id={0}",
                                            e.CommandName));
        }

        
        protected void CheckBoxInactiveCheckedChanged(object sender, EventArgs e)
        {
            if (_check == 1)
            {
                if (CheckBoxInActive.Checked && _check == 1)
                {
                    _flag = 1;
                    grvTrustDistrictList.DataSource = trustDistrictBiz.StartWithByNames(_key, _flag, trustRegionId);
                    grvTrustDistrictList.DataBind();
                }
                else if (CheckBoxInActive.Checked == false && _check == 1)
                {
                    _flag = 0;
                    grvTrustDistrictList.DataSource = trustDistrictBiz.StartWithByNames(_key, _flag, trustRegionId);
                    grvTrustDistrictList.DataBind();

                }

            }
            else if (_check == 0)
            {
                if (CheckBoxInActive.Checked && _check == 0)
                {
                    grvTrustDistrictList.DataSource = trustDistrictBiz.GetTrustDistrictInActive(trustRegionId);
                    grvTrustDistrictList.DataBind();
                }
                else
                {
                    _flag = 0;
                    LoadAllTrustDistrict();
                }
            }
        }

        protected void LbtnTrustDistrictName_Command(object sender, CommandEventArgs e)
        {

            if (e.CommandArgument.Equals("False"))
            {
                int trustDistrictId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                bool result = trustDistrictBiz.SetActiveTrustDistrict(trustDistrictId, true);
            }

            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/TrustDistrictDetails.aspx?id={0}",
                                            e.CommandName));
        }

        protected void grvTrustDistrictList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvTrustDistrictList.PageIndex = e.NewPageIndex;
            if (_check == 1)
            {
                if (CheckBoxInActive.Checked && _check == 1)
                {
                    _flag = 1;
                    grvTrustDistrictList.DataSource = trustDistrictBiz.StartWithByNames(_key, _flag, trustRegionId);
                }
                else if (CheckBoxInActive.Checked == false && _check == 1)
                {
                    _flag = 0;
                    grvTrustDistrictList.DataSource = trustDistrictBiz.StartWithByNames(_key, _flag, trustRegionId);

                }
            }
            else if (_check == 0)
            {
                if (CheckBoxInActive.Checked && _check == 0)
                {
                    grvTrustDistrictList.DataSource = trustDistrictBiz.GetTrustDistrictInActive(trustRegionId);
                }
                else
                {
                    _flag = 0;
                    grvTrustDistrictList.DataSource = trustDistrictBiz.GetTrustDistrictById1(trustRegionId);
                }
            }
            grvTrustDistrictList.DataBind();
        }

        protected void grvTrustDistrictList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/Forms/AddTrustDistrict.aspx");
        }
    }
}